package com.sdry.web.controller.zc.quality;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sdry.model.hpf.Page;
import com.sdry.model.llm.Department;
import com.sdry.model.llm.User;
import com.sdry.model.zc.ChangeNotice;
import com.sdry.service.zc.ChangeNoticeService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * @ClassName: ChangeNoticeController 
 * @Description:工艺变更通知单Controller
 * @Author zc
 * @Date 2018年11月2日 下午4:12:04 
 * @version 1.0
 */
@RequestMapping(value="/changeNotice")
@Controller
public class ChangeNoticeController {

	@Resource
	private ChangeNoticeService changeNoticeService;
	
	/**
	 * 查询工艺变更通知单
	 * @param changeNotice
	 * @param response
	 */
	@RequestMapping(value="/findChangeNoticeByAdcID")
	public void findChangeNoticeByAdcID(ChangeNotice changeNotice,HttpServletResponse response){
		ChangeNotice changeNotice2 = changeNoticeService.findChangeNoticeByAdcID(changeNotice);
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(changeNotice2, jsonConfig);
		try {
			ResponseUtil.write(response, jsonArray);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 分页查询工艺变更通知单
	 * @param page 分页条件
	 * @param changeNotice 查询条件
	 * @param response
	 */
	@RequestMapping(value="/listPageChangeNotice")
	public void listPageLineSideLibrary(Page page,ChangeNotice changeNotice,HttpServletResponse response){
		Map<String, Object> map = new HashMap<>();
		map.put("page", page.getPage());
		map.put("limit", page.getLimit());
		if (changeNotice.getNoticeNum() != null) {
			map.put("noticeNum", changeNotice.getNoticeNum());
		} else {
			map.put("noticeNum", "");
			changeNotice.setNoticeNum("");
		}
		if (changeNotice.getReserve5() != null) {
			map.put("reserve5", changeNotice.getReserve5());
		} else {
			map.put("reserve5", "");
			changeNotice.setReserve5("");;
		}
		if (changeNotice.getReserve1() != null) {
			map.put("reserve1", changeNotice.getReserve1());
		} else {
			map.put("reserve1", "");
			changeNotice.setReserve1("");;
		}
		List<ChangeNotice> changeNoticeList = changeNoticeService.listPageChangeNotice(map);
		int count = changeNoticeService.countChangeNotice(changeNotice);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(changeNoticeList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 保存工艺变更通知单
	 * @param changeNotice
	 * @param type
	 * @param response
	 */
	@RequestMapping(value="/saveChangeNotice")
	public void saveAbnormalDisposalCraft(ChangeNotice changeNotice,String type,HttpServletResponse response,HttpSession session){
		int isSuccess = 0;
		User user = (User) session.getAttribute("currentUser");
		Department department = (Department) session.getAttribute("department");
		if (changeNotice.getNoticeID() == null || "".equals(changeNotice.getNoticeID())) {
			changeNotice.setDraftPerson(user.getUserName());
			changeNotice.setDraftDepartment(department.getName());
			changeNotice.setReserve1("0");
			changeNotice.setReserve5("1");
			isSuccess = changeNoticeService.insertChangeNotice(changeNotice);
		} else {
			ChangeNotice c = changeNoticeService.findChangeNoticeByID(changeNotice.getNoticeID());
			if (changeNotice.getReserve2() != null && !"".equals(changeNotice.getReserve2())) {
				c.setReserve2(changeNotice.getReserve2());
				c.setqSignature(changeNotice.getqSignature());
			}
			if (changeNotice.getReserve3() != null && !"".equals(changeNotice.getReserve3())) {
				c.setReserve3(changeNotice.getReserve3());
				c.settSignature(changeNotice.gettSignature());
			}
			if (changeNotice.getReserve4() != null && !"".equals(changeNotice.getReserve4())) {
				c.setReserve4(changeNotice.getReserve4());
				c.setMaSignature(changeNotice.getMaSignature());
			}
			if (changeNotice.getmOpinion() != null && !"".equals(changeNotice.getmOpinion())) {
				c.setmOpinion(changeNotice.getmOpinion());
			}
			if ("1".equals(type)) {
				c.setReserve1("3");
			} else {
				if ("2".equals(changeNotice.getReserve5())) {
					c.setReserve1("1");
					c.setqSignature(user.getUserName());
				}
				if ("3".equals(changeNotice.getReserve5())) {
					c.setReserve1("1");
					c.settSignature(user.getUserName());
				}
				if ("4".equals(changeNotice.getReserve5())) {
					c.setReserve1("1");
					c.setMaSignature(user.getUserName());
				}
				if ("5".equals(changeNotice.getReserve5())) {
					c.setReserve1("2");
					c.setmSignature(user.getUserName());
				}
			}
			c.setReserve5(changeNotice.getReserve5());
			isSuccess = changeNoticeService.updateChangeNotice(c);
		}
		
		try {
			ResponseUtil.write(response, isSuccess);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 查询工艺变更通知单
	 * @param noticeID
	 * @param response
	 */
	@RequestMapping(value="/findChangeNoticeByID")
	public void findChangeNoticeByID(Long noticeID,HttpServletResponse response){
		ChangeNotice changeNotice = changeNoticeService.findChangeNoticeByID(noticeID);
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(changeNotice, jsonConfig);
		try {
			ResponseUtil.write(response, jsonArray);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
